Tuesday, January 12, 2010

Use Find Method of Generic List

Is there a quick way to Find an object in a List<T> without looping through the List or using Linq? Yes; use the Find Method of the object.

If you are new to .NET, or even if you have been using it for years, but you still have outdated ways of doing things, you might be waisting developement time, resources, and performance if you do not keep up to date on new features that the .NET framework brings to the table for algorithms that you use all the time.

For example, if you need to find an object in a generic List<T>, you might still be using code like the code shown below. Suppose that you have a List<Address> and the Address object has an IsPrimary field (bool). One and only one of the objects can be primary and you need to retrieve the primary address object from the List. The "user" object has an Addressess property which is a List<Address>. The tried and proven (over and over) method for doing this would be:

   Address primaryAddress;


   foreach (Address addr in user.Addresses)
   {
       if (addr.IsPrimary)
       {
         primaryAddress = addr;
         break;
       } // if
   } // foreach


Obviously, the code shown above works just fine. But, consider the following line of code that will achieve the same result.


   Address primaryAddr =
   user.Addresses.Find(delegate(Address obj)
      { return obj.IsPrimary; });


Passing a Delegate to the Find method of the generic List<Address> finds the object that has the IsPrimary field set to true with a single line of code.

Just another shortcut that hopefully will save you some time.

Need to automatically organize your code windows? You'll be amazed how easy it is to keep the code in your code windows organized. TRY IT FREE FOR 30 DAYS BY CLICKING HERE.



Automatically generate braces in C#! Try CSharpCompleter and stop wasting valuable time needlessly typing hundreds of braces {} daily. Try CSharpCompleter for 30 DAYS FREE.



You can also email me directly at les@KnowDotNet.com.

Using a LIKE Operator in a Stored Procedure

How do I code the Stored Procedure and the parameter to it to use the LIKE operator?
The answer should seem quite simple, but it is not exactly as you would do it in dynamic sql. It is almost the same with a subtile difference. In dynamic sql, you might do something like this:

   string searchText = "register";
   string sql = "select * from FAQs where QuestionColumn LIKE '%"
      + searchText + "%'";


to create the dynamic sql statement, and it will work fine. However, it does not appear that passing a parameter value like the following code works in the Stored Procedure; at least it failed to make a find for me, even though I had copied the code from VBScript in an old ASP page, where it was working.


string searchString = "'%register%'";
   sqlCommand.Parameters.Add(new SqlParameter("@SearchString",
      SqlDbType.VarChar, 50));
   sqlCommand.Parameters[0].Value = searchString;


and the code in the stored procedure was:


   SELECT Question, Answer
   FROM SP_FAQs
   WHERE Question LIKE @SearchString
      OR Answer LIKE @SearchString


So, to get it to work, I changed the parameter setting code to:


    string searchString = "register";
   sqlCommand.Parameters.Add(new SqlParameter("@SearchString",
      SqlDbType.VarChar, 50));
   sqlCommand.Parameters[0].Value = searchString;


and the Stored Procedure code to the following:


   SELECT Question, Answer
   FROM SP_FAQs
   WHERE Question LIKE '%' + @SearchString + '%'
      OR Answer LIKE '%' + @SearchString + '%'


The subtile difference is that the adding of the "%" to the beginning and end of the parameter is done inside the Stored Procedure instead of passing it in with the parameter. Even if somehow I made a simple mistatke that was causing the search to fail, it is still better to have the stored procedure do the work of enclosing the searchString in "%" than for each call have to worry about passing in anything but the actual text for which to search.


Need to automatically organize your code windows? You'll be amazed how easy it is to keep the code in your code windows organized. TRY IT FREE FOR 30 DAYS BY CLICKING HERE.


Automatically generate braces in C#! Try CSharpCompleter and stop wasting valuable time needlessly typing hundreds of braces {} daily. Try CSharpCompleter for 30 DAYS FREE.



You can also email me directly at les@KnowDotNet.com.